package org.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;

public class DBUtils {
    private static String dbUrl = "jdbc:postgresql://localhost:5432/";
    private static String dbName = "s21_bootcamp";
    private static String dbUser = "nedrapro_psql";
    private static String dbPass = "WaSq!32435";

    /**
     * Создание соединения с базой данных
     *
     * @return текущее соединение
     */
    public static Connection connectToDb() {
        Connection con_obj = null;

        try {
            con_obj = DriverManager.getConnection(dbUrl + dbName, dbUser, dbPass);
            if(con_obj != null) {
                CRUDUtils.logger.log(Level.INFO, "Connection established successfully");
                //System.out.println("Connection established successfully!");
            }
            else {
                CRUDUtils.logger.log(Level.INFO, "Connection failed");
                //System.out.println("Connection failed !!");
            }
        }
        catch (SQLException e) {
            CRUDUtils.logger.log(Level.WARNING, e.getMessage());
            e.printStackTrace();
            //System.out.println(e.getMessage());
        }
        return con_obj;
    }
}
